<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                template="/WEB-INF/template.xhtml">

    <ui:define name="title">
        ExceptionHandler
    </ui:define>

    <ui:define name="description">
        PrimeFaces provides a powerful ExceptionHandler out of the box featuring support for ajax and non-ajax requests, ability to use error-page
        configuration in web.xml, an EL extension called #{pfExceptionHandler} to provide information about the exception and the p:ajaxExceptionHandler
        component to handle ajax exceptions at the same page.
    </ui:define>

    <ui:param name="documentationLink" value="/core/errorhandling" />

    <ui:define name="implementation">
        
        <h:form>
            <h3 style="margin-top:0">AJAX</h3>
            <p:commandButton action="#{exceptionHandlerView.throwViewExpiredException}"
                             ajax="true"
                             value="Throw ViewExpiredException!" />
            <p:commandButton action="#{exceptionHandlerView.throwNullPointerException}"
                             ajax="true"
                             value="Throw NullPointerException!" />
            <!-- IllegalStateException is not handled using ajaxExceptionHandlers below, so the error page is shown-->
            <p:commandButton action="#{exceptionHandlerView.throwWrappedIllegalStateException}"
                             ajax="true"
                             value="Throw IllegalStateException!" />

            <h3>Non-AJAX</h3>
            <p:commandButton action="#{exceptionHandlerView.throwViewExpiredException}"
                             ajax="false"
                             value="Throw ViewExpiredException!" />
            <!-- NullPointerException has no specific error-page defined in web.xml compared to ViewExpiredException -->
            <!-- https://github.com/primefaces/showcase-facelift/blob/master/src/main/webapp/WEB-INF/web.xml -->
            <p:commandButton action="#{exceptionHandlerView.throwNullPointerException}"
                             ajax="false"
                             value="Throw NullPointerException!" />


            <p:ajaxExceptionHandler type="javax.faces.application.ViewExpiredException"
                                    update="exceptionDialog"
                                    onexception="PF('exceptionDialog').show();" />
            
            <p:ajaxExceptionHandler type="java.lang.NullPointerException"
                                    update="exceptionDialog"
                                    onexception="PF('exceptionDialog').show();" />

            <p:dialog id="exceptionDialog" header="Exception '#{pfExceptionHandler.type}' occured!" widgetVar="exceptionDialog"
                      height="500px">
                Message: #{pfExceptionHandler.message} <br/>
                StackTrace: <h:outputText value="#{pfExceptionHandler.formattedStackTrace}" escape="false" /> <br />

                <p:button onclick="document.location.href = document.location.href;"
                          value="Reload!"
                          rendered="#{pfExceptionHandler.type == 'javax.faces.application.ViewExpiredException'}" />
            </p:dialog>
        </h:form>
        
    </ui:define>

</ui:composition>